//
//  NH_DataDownloadTools.m
//  UILesson17_ImageDownloader
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 宁浩. All rights reserved.
//

#import "NH_DataDownloadTools.h"

@implementation NH_DataDownloadTools

//block 把一段代码当做参数来用
+(void)downloadDataWithURL:(NSString *)urlString andMethod:(NSString *)method andBody:(NSString *)body andBlock:(optionBlock)block{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    //更好的方式忽略大小写进行比较
    if ([method isEqualToString:@"POST"] || [method isEqualToString:@"post"]) {
        //这里添加POST方式需要设置的东西
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        block(data);
    }];
}
@end
